package engine.dojo.element;

import org.json.JSONException;
import org.json.JSONObject;

public class ButtonManager {

	public static JSONObject specializeButton(JSONObject element){
			
			JSONObject returnAttributes = null;
			JSONObject elementAttributes = null;
			
			try{
				elementAttributes = element.getJSONObject("attributes");
			
				if(elementAttributes.getString("buttonAction").equals("sendForm")){
						
					JSONObject attributes = new JSONObject();
					returnAttributes = new JSONObject();
					
					String label = elementAttributes.getString("label");
					String wsURL = elementAttributes.getString("wsURL");
					String formToSend = elementAttributes.getString("formToSend");
					
					attributes.put("label", label);
					attributes.put("wsURL", wsURL);
					attributes.put("formToSend", formToSend);
					
					//there is a bug with dojo.parser, if i use the onclick event, when user click on the button it call the function twice
					//see http://bugs.dojotoolkit.org/ticket/13386
					attributes.put("data-dojo-props", "onClick:function(e){buttonSendFormClick('" +element.getString("id")+ "');}");
					
					returnAttributes.put("attributes", attributes);
					
				}
				
			}catch(JSONException e){
				e.printStackTrace();
			}
			
			return returnAttributes;
	}
}
